package com.xjust.service.impl;

import com.xjust.mapper.CourseMapper;
import com.xjust.service.CourseService;
import com.xjust.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    //获取所有选课课程
    public List<Map<String, Object>> getAllCourse() {
        List<Map<String, Object>> list =courseMapper.selectAllCourse();
        return list;
    }

    @Override
    //根据课程Id查找相应班级
    public List<Map<String, Object>> getClassBycourse(Integer courseId) {
        List<Map<String, Object>> list =courseMapper.selectClassBycourse(courseId);
        return list;
    }
    //提交选课信息至选课表
    @Override
    public void insertCourseSelection(Map<String, Object> courseInfo) {
        //解析token
        String token = request.getHeader("Authorization");
        Claims t = JwtUtils.parseJWT(token);
        System.out.println(t);
    courseInfo.put("studentId",t.get("id"));
        //修改班级Id对应人数
        courseMapper.updateCourseStudentNum(courseInfo.get("classId"));
        courseMapper.insertCourseSelection(courseInfo);
    }
    //根据学生ID查询选课信息
    @Override
    public ArrayList<Integer> getCourseSelect() {
        //解析token
        String token = request.getHeader("Authorization");
        Claims t = JwtUtils.parseJWT(token);
        Integer id = (Integer) t.get("id");
        List<Map<String,Object>> list = courseMapper.selectCourseSelectByStuId(id);
        ArrayList<Integer> courseSelect = new ArrayList<>();
        //将查询到的选课信息添加至数组中
        for (Map<String, Object> map : list) {
           courseSelect.add(courseMapper.selectCourseById((Integer) map.get("course_id")));
        }
        return courseSelect;
    }
}
